package com.squeekeek.util.qrcode.Ui;

import com.squeekeek.util.qrcode.R;

import android.app.Activity;
import android.hardware.Camera;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CaptureButtonClickListener implements OnClickListener {
	public static final int STATE_FROZEN = 0x00;
	public static final int STATE_RUNNING = 0x01;
	
	private Camera myCamera;
	private int viewState;
	ImageCaptureCallback myCaptureCallback = null;
	
	public CaptureButtonClickListener(Camera c, Activity a) {
		this.myCamera = c;
		this.myCaptureCallback = new ImageCaptureCallback(a);
		this.viewState = STATE_RUNNING;
	}
	
	public void setPreviewState(int s) {
		this.viewState = s;
	}
	
	@Override
	public void onClick(View v) {
		if(this.myCamera != null) {
			try {
				switch(viewState) {
				case STATE_RUNNING:
					// Preview is automatically frozen
					((Button) v).setText(R.string.actstr_recapturebtn);
					
					// Asynchronous! :O
					this.myCamera.takePicture(null, null, this.myCaptureCallback);
					this.viewState = STATE_FROZEN;
					break;
				case STATE_FROZEN:
					((Button) v).setText(R.string.actstr_capturebtn);
					
					// Preview is frozen. Restart it.
					myCamera.startPreview();
					this.viewState = STATE_RUNNING;
					break;
				}
			}	
			catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
}
